package lu.med.praxiswahl.wahlumed.service.beans;

import java.util.Calendar;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.swing.JOptionPane;

import lu.med.praxiswahl.wahlumed.dao.beans.daointerface.HollidayDAOInt;
import lu.med.praxiswahl.wahlumed.model.beans.HollidaysTBL;
import lu.med.praxiswahl.wahlumed.service.beans.serviceinterface.HollidayServiceInt;

@Stateless
public class HollidayService implements HollidayServiceInt {

	private HollidaysTBL tmpHoli;

	@EJB
	private HollidayDAOInt holiDao;

	public HollidayService() {
		tmpHoli = new HollidaysTBL();
	}

	@Override
	public boolean are_UrlaubTage(HollidaysTBL[] datum) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean is_FeierTag() {
		return tmpHoli.isFeiertag();
	}

	@Override
	public boolean are_FeierTage(HollidaysTBL[] datum) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean is_UrlaubTag() {
		return tmpHoli.isUrlaub();
	}

	@Override
	public int mark_day_as_feiertag() {
		tmpHoli.setFeiertag(true);
		holiDao.update(tmpHoli);
		return RET_OPERATION_DONE_PROPERLY;
	}

	@Override
	public int mark_day_as_urlaub() {
		tmpHoli.setUrlaub(true);
		holiDao.update(tmpHoli);
		return RET_OPERATION_DONE_PROPERLY;
	}

	@Override
	public int remove_day(Long id) {
		holiDao.delete(id);
		return RET_OPERATION_DONE_PROPERLY;
	}

	@Override
	public int create_holliday(Calendar datum, boolean feiertag, boolean urlaub) {
		HollidaysTBL tmpHoli_1 = new HollidaysTBL();
		tmpHoli_1.setDatum(datum);
		tmpHoli_1.setFeiertag(feiertag);
		tmpHoli_1.setUrlaub(urlaub);
		this.holiDao.persist(tmpHoli_1);
		return RET_OPERATION_DONE_PROPERLY;
	}

	@Override
	public void getA_HollidayInstance(HollidaysTBL holliday) {
		tmpHoli = holliday;

	}

	@Override
	public HollidaysTBL[] printFeiertage() {
		if (holiDao == null) {
			JOptionPane.showMessageDialog(null, "Dao is null");
		}
		Object[] tmp = holiDao.listFeierTage().toArray();
		HollidaysTBL[] hollidays = new HollidaysTBL[tmp.length];

		for (int i = 0; i < tmp.length; i++) {
			hollidays[i] = (HollidaysTBL) tmp[i];
		}
		return hollidays;
	}

	@Override
	public HollidaysTBL[] printUrlaubeTage() {
		Object[] tmp = holiDao.listUrlaubTage().toArray();
		HollidaysTBL[] hollidays = new HollidaysTBL[tmp.length];

		for (int i = 0; i < tmp.length; i++) {
			hollidays[i] = (HollidaysTBL) tmp[i];
		}
		return hollidays;
	}

}
